﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PerformanceAssessment.Models;

namespace PerformanceAssessment.Controllers
{
    public class ObjectivesController : Controller
    {
        private PerformanceAssessmentDB db = new PerformanceAssessmentDB();
        //
        // GET: /Objectives/

        public ActionResult Index(string teamString)
        {
            var TeamLst = new List<string>();
            var TeamQry = from d in db.Teams
                          orderby d.ID
                          select d.Name;
            TeamLst.AddRange(TeamQry.Distinct());
            ViewBag.teamString = new SelectList(TeamLst);
            var allEmployees = db.Employees.ToList();
            var allTeams = db.Teams.ToList();
            var employees = from u in allEmployees
                            join t in allTeams on u.IdTeam equals t.ID
                            select new EmployeeList
                            {
                                ID = u.ID,
                                Team = t.Name,
                                Name = u.FirstName + " " + u.FirstLastName + " " + u.SecondLastName,
                                CanEditGoal = (bool) (u.CanEditGoal == true ? u.CanEditGoal : false)
                            };
            if (!String.IsNullOrEmpty(teamString))
            {
                employees = employees.Where(x => x.Team == teamString);
            }
            employees = employees.OrderBy(x => x.Name).ToList();

            return View(employees as List<EmployeeList>);
        }
        [HttpPost]
        public ActionResult SaveIndex(List<EmployeeList> model)
        {
            foreach(var item in model)
            {
                var employee = db.Employees.FirstOrDefault(x => x.ID == item.ID);
                employee.CanEditGoal = item.CanEditGoal;
            }
            db.SaveChanges();

            return RedirectToAction("Index");
        }

    }
}
